package com.neborosoft.android.events;

import com.neborosoft.lib.collections.stack.HistoryStack;
import com.neborosoft.lib.handlers.OnHardwareBackPressed;

/**
 * Created with IntelliJ IDEA.
 * User: CM
 * Date: 03.03.13
 * Time: 19:01
 * To change this template use File | Settings | File Templates.
 */
public class HistoryManager {
    private static HistoryManager instance;

    private HistoryStack<OnHardwareBackPressed> onHardwareBackPressedStack;

    public static HistoryManager getInstance(){
        if(instance == null){
            throw new IllegalStateException("call init before getInstance");
        }

        return instance;
    }

    public static void init(int size){
        instance = new HistoryManager();
        instance.onHardwareBackPressedStack = new HistoryStack<OnHardwareBackPressed>(size);
    }

    public void pushHardwareBackPressedListener(OnHardwareBackPressed onHardwareBackPressed){
        onHardwareBackPressedStack.push(onHardwareBackPressed);
    }

    public void popHardwareBackPressedListener(){
        if(onHardwareBackPressedStack.isEmpty()){
            return;
        }

        OnHardwareBackPressed onHardwareBackPressed = onHardwareBackPressedStack.pop();
        onHardwareBackPressed.onBack();
    }
}
